<?php

namespace common\widgets\ajaxbtn;

use yii\base\Widget;
use yii\web\NotFoundHttpException;

/**
 * @example <a href="javascript:<?= Confirm::widget(['tip'=>'是否确定删除该数据？','url'=>Url::to(['article/delete'])]) ?>" target="_self">删除</a>
 * @author Administrator
 *
 */
class Confirm extends Widget{
    
    public $tip;
    public $url;
    
    public function init(){
        parent::init();
        if(empty($this->url)){
            throw new NotFoundHttpException('url参数必须');
        }
        if(empty($this->tip)){
            throw new NotFoundHttpException('tip参数必须');
        }
        $index = strripos($this->url, '?');
        $csrf = \Yii::$app->request->csrfToken;
        if ($index === false) {
            $this->url = $this->url."?_csrf=".$csrf;
        } else {
            $this->url = $this->url."&_csrf=".$csrf;
        }
        AjaxAsset::register($this->view);
    }
    
    public function run(){
        $str .= "toConfirm('".$this->tip."','".$this->url."')";
        return $str;
    }
    

}